<?php

namespace App\Http\Controllers\Home;

use App\Model\Order;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $value = $request->session()->get('user');

        $uid = $value['uid'];


        $info = Order::where('o_uid',$uid)->get();

        $onumber = [];
        $onumber1 = [];
        $onumber0 = [];
        $onumber00 = [];
        $data_all =[];
        $data_all1 =[];
        $data_all0 =[];
        $data_all00 =[];
        foreach($info as $k=>$v){

            $onumber[] = $v['onumber'];
        }
         $onumber = array_unique($onumber);


        foreach($onumber as $k=>$v ){

            $info = Order::where('onumber',$v)->where('o_uid',$uid)->get();
            foreach($info as $kk=>$vv){
                $a = Order::find($vv['oid'])->good()->first();

               // $b = Order::where('onumber',$v)->where('o_uid',$uid)->get();


                    $a['num'] = $vv['onum'];

                    $data_all[$k]['otime']=$vv['otime'];
                    $data_all[$k]['status'] = $vv['ostatus'];
                $data_all[$k]['price'] = $vv['o_prices'];
                //$res = array_merge($a,$b);
                $data_all[$k]['good'][$kk] = $a;
            }

            //
//            }
            $data_all[$k]['number'] = $v;
        }
        ////////////////////////////已签收///////////////////////////////

       $info1 = Order::where('o_uid',$uid)->where('ostatus',1)->get();
       // $info1 = Order::where('ostatus',1)->get();
        foreach($info1 as $k=>$v){

            $onumber1[] = $v['onumber'];
        }
        $onumber1 = array_unique($onumber1);
       // dd($onumber1);
        foreach($onumber1 as $k=>$v ){

            $info1 = Order::where('onumber',$v)->where('o_uid',$uid)->where('ostatus',1)->get();
            foreach($info1 as $kk=>$vv){
                $a = Order::find($vv['oid'])->good()->first();

                // $b = Order::where('onumber',$v)->where('o_uid',$uid)->get();


                $a['num'] = $vv['onum'];

                $data_all1[$k]['otime']=$vv['otime'];
                $data_all1[$k]['status'] = $vv['ostatus'];
                $data_all1[$k]['price'] = $vv['o_prices'];
                //$res = array_merge($a,$b);
                $data_all1[$k]['good'][$kk] = $a;
            }

            //
//            }
            $data_all1[$k]['number'] = $v;
        }

///////////////////////////未签收//////////////////////////////////

        $info0 = Order::where('o_uid',$uid)->where('ostatus',0)->get();
        // $info1 = Order::where('ostatus',1)->get();
        foreach($info0 as $k=>$v){

            $onumber0[] = $v['onumber'];
        }
        $onumber0 = array_unique($onumber0);
        // dd($onumber1);
        foreach($onumber0 as $k=>$v ){

            $info0 = Order::where('onumber',$v)->where('o_uid',$uid)->where('ostatus',0)->get();
            foreach($info0 as $kk=>$vv){
                $a = Order::find($vv['oid'])->good()->first();

                // $b = Order::where('onumber',$v)->where('o_uid',$uid)->get();


                $a['num'] = $vv['onum'];

                $data_all0[$k]['otime']=$vv['otime'];
                $data_all0[$k]['status'] = $vv['ostatus'];
                $data_all0[$k]['price'] = $vv['o_prices'];
                //$res = array_merge($a,$b);
                $data_all0[$k]['good'][$kk] = $a;
            }

            //
//            }
            $data_all0[$k]['number'] = $v;
        }

        ///////////////////////////////////////////////////////
        ///////////////////待评价///////////////////////////////////

        $info00 = Order::where('o_uid',$uid)->where('ostatus',1)->where('oevaluate',0)->get();

        // $info1 = Order::where('ostatus',1)->get();
        foreach($info00 as $k=>$v){

            $onumber00[] = $v['onumber'];
        }
        $onumber00 = array_unique($onumber00);
        // dd($onumber1);
        foreach($onumber00 as $k=>$v ){

            $info00 = Order::where('onumber',$v)->where('o_uid',$uid)->where('oevaluate',0)->get();
            foreach($info00 as $kk=>$vv){
                $a = Order::find($vv['oid'])->good()->first();

                // $b = Order::where('onumber',$v)->where('o_uid',$uid)->get();


                $a['num'] = $vv['onum'];

                $data_all00[$k]['otime']=$vv['otime'];
                $data_all00[$k]['status'] = $vv['ostatus'];
                $data_all00[$k]['price'] = $vv['o_prices'];

                //$res = array_merge($a,$b);
                $data_all00[$k]['good'][$kk] = $a;
            }

            //
//            }
            $data_all00[$k]['number'] = $v;
        }

        /////////////////////////////////////////////////
//dd($data_all00);
          return view('/home/order',['data'=>$data_all,'data1'=>$data_all1,'data0'=>$data_all0,'data00'=>$data_all00]);
    }

    /**
     * 确认收货
     */
    public function ok ($number){

       $res = Order::where('onumber', $number)
            ->update(['ostatus' => 1]);
        if($res){
            return redirect('/home/order')->with('success','收货成功');
        }else{
            return back()->with('error','收货失败');
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($number)
    {
        //
        $data = Order::where('onumber', $number)->get();
        $ndata = Order::where('onumber', $number)->first();
        foreach($data as $k=>$v){
            $data[$k]['good'] = Order::find($v['oid'])->good()->first();
        }
        //dd($data);
        return view('/home/orderinfo',['data'=>$data,'ndata'=>$ndata]);
    }

    public function show0($number)
    {
        //
        $data = Order::where('onumber', $number)->get();
        $ndata = Order::where('onumber', $number)->first();
        foreach($data as $k=>$v){
            $data[$k]['good'] = Order::find($v['oid'])->good()->first();
        }
        //dd($data);
        return view('/home/orderinfo0',['data'=>$data,'ndata'=>$ndata]);

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
